package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Standardtype;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("StandardtypeController")
public class StandardtypeController extends CmiEmaController {

	@RequestMapping("/listStandardtypes")
	public ModelAndView listStandardtypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = standardtypePaginationService.updatePagination(request, pagination);
		Set<Standardtype> standardtypes = standardtypePaginationService.getPaginationStandardtypes(pagination);

		mav.addObject("standardtypes", standardtypes);
		mav.addObject("entityName", "standardtype");
		mav.addObject("pagination", pagination);
		mav.setViewName("standardtype/listStandardtypes");
		return mav;
	}

	public String listStandardtypes() {
		return "redirect:/listStandardtypes";
	}

	@RequestMapping("/viewStandardtype")
	public ModelAndView viewStandardtype(@RequestParam Integer standardtypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("standardtype", standardtypeDAO.findStandardtypeByPrimaryKey(standardtypeId));
		mav.setViewName("standardtype/viewStandardtype");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteStandardtypes")
	public ModelAndView confirmDeleteStandardtypes(
			@RequestParam(value = "deleteStandardtypesIds", required = true) Set<Integer> deleteStandardtypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Standardtype> deleteStandardtypes = standardtypeDAO.findStandardtypesByIds(deleteStandardtypesIds);
		Set<Standardtype> referencedStandardtypes = standardtypeService.loadReferenceStandardtypes(deleteStandardtypes);
		boolean hasReferenced;
		if (referencedStandardtypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteStandardtypes);
			hasReferenced = false;
		} else {
			deleteStandardtypes.removeAll(referencedStandardtypes);
			mav.addObject("referencedEntitys", referencedStandardtypes);
			mav.addObject("deleteEntitys", deleteStandardtypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Standardtype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listStandardtypes");
		return mav;
	}

	@RequestMapping("/deleteStandardtype")
	public String deleteStandardtype(@RequestParam Integer standardtypeId) {
		Standardtype standardtype = standardtypeDAO.findStandardtypeByPrimaryKey(standardtypeId);
		standardtypeService.deleteStandardtype(standardtype);
		return "forward:/listStandardtypes";
	}

	@RequestMapping("/confirmDeleteStandardtype")
	public ModelAndView confirmDeleteStandardtype(@RequestParam Integer standardtypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("standardtype", standardtypeDAO.findStandardtypeByPrimaryKey(standardtypeId));

		mav.setViewName("standardtype/deleteStandardtype");

		return mav;
	}

	@RequestMapping("/deleteStandardtypes")
	public String deleteStandardtypes(
			@RequestParam(value = "deleteStandardtypesId", required = true) Set<Integer> deleteStandardtypesId) {
		standardtypeService.deleteStandardtypes(deleteStandardtypesId);
		return "forward:/listStandardtypes";
	}

	@RequestMapping("/editStandardtype")
	public ModelAndView editStandardtype(@RequestParam Integer standardtypeId) {

		ModelAndView mav = new ModelAndView();

		Standardtype standardtype = standardtypeDAO.findStandardtypeByPrimaryKey(standardtypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("standardtype", standardtype);

		// 把所有的引用standardtype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(standardtypeService.loadStandardtypeObjectReferencedSet(standardtype));

		// 将编辑页面standardtype添加到mav中
		mav.setViewName("standardtype/editStandardtype");
		return mav;
	}

	@RequestMapping("/newStandardtype")
	public ModelAndView newStandardtype() {
		ModelAndView mav = new ModelAndView();
		Standardtype standardtype = new Standardtype();

		mav.addObject("standardtype", standardtype);

		// 把所有的引用standardtype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(standardtypeService.loadStandardtypeObjectReferencedSet(standardtype));

		mav.addObject("newFlag", true);
		mav.setViewName("standardtype/editStandardtype");

		return mav;
	}

	@RequestMapping("/saveStandardtype")
	public ModelAndView saveStandardtype(@ModelAttribute Standardtype standardtype) {

		ModelAndView mav = new ModelAndView();

		Standardtype existingStandardtype = CmiHibernateProxyUtil
				.deproxy(standardtypeDAO.findStandardtypeByPhysicalPrimaryKey(standardtype.getName()));

		// 新建记录
		if (standardtype.getId() == null) {
			if (standardtype.equals(existingStandardtype)) {

				mav.addAllObjects(standardtypeService.loadStandardtypeClassReferencedSet(standardtype));
				// mav = editContinue(mav, standardtype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("standardtype/editStandardtype");
			} else {
				standardtypeService.saveStandardtype(standardtype);
				mav.setViewName("forward:/listStandardtypes");
			}
		}
		// 编辑记录
		else {
			Standardtype originStandardtype = CmiHibernateProxyUtil
					.deproxy(standardtypeDAO.findStandardtypeByPrimaryKey(standardtype.getId()));
			if (!standardtype.equals(originStandardtype) && standardtype.equals(existingStandardtype)) {
				mav.addAllObjects(standardtypeService.loadStandardtypeClassReferencedSet(standardtype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("standardtype/editStandardtype");
			} else {
				standardtypeService.saveStandardtype(standardtype);
				mav.setViewName("forward:/listStandardtypes");
			}
		}
		return mav;
	}

	@RequestMapping("/standardtypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
